package cn.bdqn.service.impl;

import cn.bdqn.domain.Client;
import cn.bdqn.domain.Order;
import cn.bdqn.domain.OrderForm;
import cn.bdqn.mapper.ClientMapper;
import cn.bdqn.mapper.OrderFormMapper;
import cn.bdqn.mapper.OrderMapper;
import cn.bdqn.service.OrderFormService;
import cn.bdqn.service.OrderService;
import cn.bdqn.utils.result.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dkj
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private OrderFormService orderFormService;


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Order queryByPrimaryKey(Integer id) {

        //根据id查询
        Order order = orderMapper.selectByPrimaryKey(id);
        //根据订单id查询订单货物集合
        List<OrderForm> orderForms = orderFormService.queryByOrderId(id);
        //赋值
        order.setOrderForms(orderForms);
        //返回结果
        return order;

    }

    /**
     * 根据订单和付款方式模糊查询
     * @param currentPage  当前页
     * @param pageSize 每页显示数量
     * @param orderNumber 订单号
     * @param paymentType 付款类型
     * @param state 订单状态  1.处理  2.未处理
     * @return
     */
    @Override
    public Page queryByOrderNumberAndPaymentType(Integer currentPage, Integer pageSize, String orderNumber, Integer paymentType,Integer state) {




        //开启分页 currentPage > 0 ? currentPage:1 判断当前页数是否大于0
        PageHelper.startPage(currentPage > 0 ? currentPage:1,pageSize);
        //根据订单和付款方式模糊查询
        List<Order> orders = orderMapper.selectByOrderNumberAndPaymentType(orderNumber,paymentType,state);

        //分页
        PageInfo<Order> orderPages = new PageInfo<Order>(orders);
        //封装分页数据
        Page page = new Page();
        //分页之后的数据
        page.setMenuList(orderPages.getList());
        //总记录数
        page.setTotalRecord((int)orderPages.getTotal());
        //返回封装的分页对象
        return page;
    }


    /**
     * 新增
     * @param order
     * @return
     */
    @Override
    public Order saveOrder(Order order) {

        //设置订单状态
        order.setState(2);
        //根据id查询
        Client client = clientMapper.selectByPrimaryKey(order.getClientId());

        //赋值
        order.setClient(client);
        //新增
        orderMapper.insertOrder(order);

        //返回添加的对象
        return order;
    }

    /**
     * 更新
     * @param order
     */
    @Override
    public Order updateOrder(Order order) {

        //查询客户
        Client client = clientMapper.selectByPrimaryKey(order.getClientId());

        //客户
        order.setClient(client);
        //更新
        orderMapper.updateOrder(order);
        //查询更新过后的对象
        Order order1 = orderMapper.selectByPrimaryKey(order.getId());
        //返回结果
        return order1;
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteOrder(Integer id) {

        //根据id删除
        orderMapper.deleteOrder(id);
    }
}
